<?php
/*
 * Google Analytics Track & Redirect Page
 * With Link Generator and URL Shortener
*/

/* CONFIGURATION */
$gaAccount = "UA-0000000-0"; // Your Property ID
$gaDomain = "yourdomain.com"; // Your Domain Name
define("SELF", "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);

/* HTML */
$head = "<!doctype html>
<html>
	<head>
		<title>GARLG v.0.2</title>
		<script type='text/javascript' src='http://code.jquery.com/jquery-latest.js'></script>
		<script type='text/javascript'>
		$(function() {
			$.ajaxSetup ({ cache: false });
			var loadUrl = 'garlgStats.php';
			$.get(  
				loadUrl, {},  
				function(responseText){  
					$('#stats').html(responseText);  
				},  
				'html'  
			);
			$('#downloads li:odd').css('background-color', '#eef');
			$('#downloads li:even').css('background-color', '#cce');
			$('#shorten').click(function(e) {
				e.preventDefault();
				var action = $('#action option:selected').attr('value');
				var category = $('#category').val();
				var url = escape($('#url').val());
				var link = '".SELF."?gac=' + category + '&gaa=' + action + '&gal=' + url;
				// $('#gaLink').html(link);
				$.getJSON('http://clic.gs/api/?format=jsonp&url=' + escape(link) + '&callback=?',
				function(data){
					if (data.url) {
						$('#gaLink').html('<b>Short Link:</b> ' + data.url + '<br/><b>Long Link:</b> ' + link);
						$.get(  
							loadUrl,  
							{s: data.url, l: link},  
							function(responseText){  
								$('#stats').html(responseText);  
							},  
							'html'  
						); 
					}
					else if(data.error) {
						$('#gaLink').html('<span style=\"color: Red;\" id=\"warning\">' + data.error + '</span>');
					}
				});
			});
		});
		</script>";
if(isset($_GET['gac']) || isset($_GET['gaa']) || isset($_GET['gal'])) {
	// GET VARIABLES
	$category = $_GET['gac'];
	$action = $_GET['gaa'];
	$label = $_GET['gal'];
	$redirect = true;
	$head .= "
		<script type='text/javascript'>
			var _gaq = _gaq || [];
			_gaq.push(['_setAccount', '{$gaAccount}']);
			_gaq.push(['_setDomainName', '{$gaDomain}']);
			_gaq.push(['_setCustomVar', 1, '{$action}', '{$label}', 3]);
			_gaq.push(['_trackPageview']);
			$(document).ready(function() {
				var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
				ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
				var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
				if(!(_gaq.push(['_trackEvent', '{$category}', '{$action}', '{$label}', 1, true]))) throw new Error('could not track event on Google Analytics');
			});
		</script>";
}
$head .= "
		<style>
			body {background:#fff;margin:0;padding:0;text-align:center;}
			section {clear:both;margin:12px auto;width:770px;}
			header, content, footer, p {width:100%;}
			header {background:#339;color:#fff;padding:12px;}
			p {text-align:left;}
			ul, ol {margin:0;padding:0;}
			#gaLink {font-size:large;line-height:32px;}
			#downloads {list-style:none;margin:0 auto;width:180px;}
			#downloads li {margin:0;padding:8px;}
			#stats {display:block;list-style:none;margin:10px auto;padding:0;width:100%;}
			#stats li {background:#ddf;border:solid 1px #aaa;border-radius:8px;display:block;float:left;margin:8px;padding:6px 12px;}
			#stats li a {color:#666;font-family:sans-serif;font-size:16px;text-decoration:none;}
			#stats li a:hover {color:#222;}
			.center {text-align:center;}
			.copy {font-size:small;}
			.copy a {text-decoration:none;}
		</style>
	</head>
	<body>
		<section>
			<header>
				<h1 id='title'>Google Analytics Redirect Link Generator</h1>
			</header>
		</section>
		<section>
			<content>";
$foot = "
			<hr style='clear:both;float:left;position:relative;width:100%;'/>
			</content>
		</section>
		<section>
			<p><b>Google Analytics Redirect Link Generator (GARLG)</b> is a PHP/Javascript program that you can upload to your server with minimal configuration to create trackable event links for your <b>Google Analytics</b> account.</p>
			<p>This program was made possible thanks to the API's provided by <b>Google Analytics</b> and <b>clic.gs</b>. This program is distributed under the terms of the <a href='http://www.gnu.org/licenses/gpl.txt' title='GNU General Public License' target='_blank'>GNU General Public License</a>.</p>
			<h2>Download:</h2>
			<ul id='downloads'>
				<li><a href='http://clic.gs/5hYb' title='Google Analytics Redirect Link Generator'>garlg-0.2 (zip archive)</a></li>
				<li><a href='http://clic.gs/5hYe' title='Google Analytics Redirect Link Generator'>garlg-0.2 (tar archive)</a></li>
				<li><a href='http://clic.gs/5hYf' title='Google Analytics Redirect Link Generator'>garlg-0.2 (gzip archive)</a></li>
			</ul>
			<footer>
				<p class='copy center'>&copy; 2012 <a href='http://iservicesinc.net' title='I Services, Inc.'>I Services, Inc.</a></p>
				<a href='http://www.gnu.org/licenses/gpl.txt' title='GNU General Public License' target='_blank'><img src='http://www.gnu.org/graphics/gplv3-88x31.png' alt='GNU General Public License'/></a>
			</footer>
		</section>
	</body>
</html>";

/* DO NOT EDIT BELOW THIS LINE */
if(isset($_GET['src'])) {highlight_file(__FILE__);exit();}
if($redirect) {
	echo $head;
	if(!(preg_match("/^http/", $label))) $label = "http://".$label;
	$fp = fopen($label, 'r');
	if(!$fp) print("<h2>{$label} cannot be opened. Please try again later.</h2>");
	if($fp) {
		if($action == "Redirect") print("<h2>you are being redirected to: '{$label}'</h2>");
		if($action == "Download") print("<h2>you are downloading: '{$label}'</h2>");
		print("<script>setTimeout(\"window.location = '{$label}'\", 800);</script>");
	}
	echo $foot;
} else {
	if ($gaAccount == "UA-0000000-0") die($head."<h2>You must configure your <b>Google Analytics</b> Account First!</h2><p>Open this file in your text editor and set your account details in the following variables: <b>\$gaAccount</b> and <b>\$gaDomain</b> on lines 8 and 9.</p>".$foot);
	if ($_GET['ua'] != $gaAccount) die($head."<h2>You must pass your Account ID as a 'ua' variable to continue!</h2><p>To access this page you must be the <b>Google Analytics</b> account holder. If you do not know your Account (Property) ID, you can login to <b>Google Analytics</b> to find it. Then visit this page with the variable 'ua=YOUR_ACCOUNT_ID' to access the link generator.</p>".$foot);
	if ($_GET['ua'] == $gaAccount) {
		echo "$head
			<p><b>Account ID:</b> {$gaAccount} &nbsp; <b>Domain:</b> {$gaDomain}</p>
			<p>Paste the link you want to redirect to in the field below, and I'll print out a short url that will redirect through this page and track it through your <b>Google Analytics</b> account.</p>
			<select id='action'>
				<option value='Redirect'>Redirect</option>
				<option value='Download'>Download</option>
			</select>
			<input id='category' type='text' placeholder='Category'/>
			<input id='url' type='text' placeholder='http://'/>
			<input id='shorten' type='submit' />
			<p id='gaLink' class='center'></p>
			<p>You can use the generated links to embed in PDF files, emails, social networks, or any other media in order to track clicks through it. Or you can use it to track downloadable files that <b>Google Analytics</b> does not normally track.</p>
			<h4>YOUR LINKS</h4>
			<ul id='stats' class='center'></ul>
		$foot";
	}
}
?>